package com.whoisaa.wifitransfer.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.whoisaa.wifitransfer.R;
import com.whoisaa.wifitransfer.bean.FileInfo;

import java.math.BigDecimal;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @Description 传输列表适配器
 * @Author AA
 * @DateTime 16/4/28 下午4:58
 */
public class ProgressListAdapter extends BaseAdapter {

    private Context mContext;
    private List<FileInfo> mDatas;


    public ProgressListAdapter(Context context, List<FileInfo> datas) {
        this.mContext = context;
        this.mDatas = datas;
    }

    /**
     * 刷新列表中单个列表项视图
     * @param view
     * @param position
     * @param fileInfo
     */
    public void updateItem(View view, int position, FileInfo fileInfo) {
        if (view == null) {
            return;
        }

        //从View中取得Holder
        ViewHolder holder = (ViewHolder) view.getTag();
        setData(holder, fileInfo);
    }

    @Override
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas == null ? null : mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_progress, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        setData(holder, (FileInfo) getItem(position));
        return convertView;
    }

    /**
     * 填充数据
     * @param holder
     * @param fileInfo
     */
    private void setData(ViewHolder holder, FileInfo fileInfo) {
        if(fileInfo != null) {
            holder.nameView.setText(fileInfo.getFileName());
            holder.sizeView.setText(getFormatSize(fileInfo.getFileLength()));
            int progress = fileInfo.getProgress();
            if(progress == 0) {
                holder.statusView.setText("准备接收");
            } else if(progress == 100) {
                holder.statusView.setText("查看");
            } else {
                holder.statusView.setText(progress + "%");
            }
        }
    }

    /**
     * 文件大小格式转换
     * @param size
     * @return
     */
    public static String getFormatSize(long size) {
        if(size <= 0) {
            return "0B";
        }

        long kiloByte = size / 1024;
        if(kiloByte < 1) {
            return size + "B";
        }

        double megaByte = kiloByte/1024;
        if(megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "K";
        }

        double gigaByte = megaByte/1024;
        if(gigaByte < 1) {
            BigDecimal result2  = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "M";
        }

        double teraBytes = gigaByte/1024;
        if(teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "G";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "T";
    }

    public class ViewHolder {

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        @Bind(R.id.iv_item_progress_img)
        ImageView imageView;

        @Bind(R.id.tv_item_progress_name)
        TextView nameView;

        @Bind(R.id.tv_item_progress_size)
        TextView sizeView;

        @Bind(R.id.tv_item_progress_status)
        TextView statusView;

    }
}
